package com.tgb.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import com.tgb.entity.Teacher;
import com.tgb.entity.TeacherDTO;
import com.tgb.manager.TeacherManager;
import com.tgb.manager.UserManager;

@Controller  
@RequestMapping("/main") 
public class MainController {
	
	@Autowired
    private UserManager userManager; 
	@Autowired
    private TeacherManager teacherManager;
	
	@RequestMapping("/list")
	public String getAll(HttpServletRequest request){
    	// Retrieve all persons
    	List<Teacher> teachers=teacherManager.getAllTeacher();
    	
    	// Prepare model object
    	List<TeacherDTO> teachersDTO = new ArrayList<TeacherDTO>();
    	
    	for (Teacher teacher: teachers) {
    		// Create new data transfer object
    		TeacherDTO dto = new TeacherDTO();
    		
			dto.setId(teacher.getId());
			dto.setName(teacher.getName());
			dto.setUsers(userManager.getAllUser(teacher.getId()));
			
            teachersDTO.add(dto);
    	}
    	request.setAttribute("teachers", teachersDTO);
		return "index";
	}
	

}
